package com.neuedu.his.dao;


import com.neuedu.his.common.utils.DBUtils;
import com.neuedu.his.entity.Register;

import java.util.List;
import java.util.Map;

public class RegisterDao {

    /**
    * 查询集合
    */
    public List<Register> selectList(String where , Object ... params){
        String sql =" select * from register ";
        if(where != null && !"".equals(where)){
            sql += where;
        }
        List<Register> registers = DBUtils.selectList(Register.class, sql, params);

        return registers;
     }

    /**
    * 分页查询
    * @param where
    * @param pageNo
    * @param size
    * @param params
    * @return
    */
    public List<Register> selectPage(String where , int pageNo,int size ,Object ... params){
        String sql =" select * from register ";
        if(where != null && !"".equals(where)){
        sql += where;
        }

        sql += " limit "+(pageNo-1)*size+"   ,"+size;

        System.out.println("sql = " + sql);
        List<Register> registers = DBUtils.selectList(Register.class, sql, params);

        return registers;
    }


    /**
    * 查询单条结果
    */
    public Register selectOne(String where ,Object ... params){
        String sql =" select * from register ";
        if(where != null && !"".equals(where)){
            sql += where;
        }
        return DBUtils.selectOne(Register.class, sql, params);
    }


    /**
     * 查询总条数
     */
    public long selectCount(String where ,Object ... params){
        String sql =" select * from register ";
        if(where != null && !"".equals(where)){
            sql += where;
        }

        sql = "select count(1) count from ("+ sql  +") tab   ";
        Map map = DBUtils.selectMap( sql , params);
        long count = (long) map.get("count");
        return count;
    }




    /**
    * 根据数据进行插入
    * @param register
    * @return
    */
    public int insert(Register register){
        String sql = "insert into register(       age   ,       allergy   ,       birthday   ,       book   ,       dept_id   ,       disease   ,       drug   ,       fee   ,       gender   ,       history   ,       name   ,       present   ,       present_treat   ,       readme   ,       regist_level   ,       status   ,       suit   ,       userid   ,       visit_time   ) values(   ?   ,     ?   ,     ?   ,     ?   ,     ?   ,     ?   ,     ?   ,     ?   ,     ?   ,     ?   ,     ?   ,     ?   ,     ?   ,     ?   ,     ?   ,     ?   ,     ?   ,     ?   ,     ?    )";
        return DBUtils.update( sql,    register.getAge()    ,     register.getAllergy()    ,     register.getBirthday()    ,     register.getBook()    ,     register.getDeptId()    ,     register.getDisease()    ,     register.getDrug()    ,     register.getFee()    ,     register.getGender()    ,     register.getHistory()    ,     register.getName()    ,     register.getPresent()    ,     register.getPresentTreat()    ,     register.getReadme()    ,     register.getRegistLevel()    ,     register.getStatus()    ,     register.getSuit()    ,     register.getUserid()    ,     register.getVisitTime() );
    }


    /**
    * 根据主键更新一条
    * @param register
    * @return
    */
    public int updateById(Register register){
        String sql = " update register set     age = ?   ,       allergy = ?   ,       birthday = ?   ,       book = ?   ,       del = ?   ,       dept_id = ?   ,       disease = ?   ,       drug = ?   ,       fee = ?   ,       gender = ?   ,       history = ?   ,       name = ?   ,       present = ?   ,       present_treat = ?   ,       readme = ?   ,       regist_level = ?   ,       status = ?   ,       suit = ?   ,       userid = ?   ,       visit_time = ?   where  regist_id = ?  ";

        return DBUtils.update( sql,
                    register.getAge()  ,
                    register.getAllergy()  ,
                    register.getBirthday()  ,
                    register.getBook()  ,
                    register.getDel()  ,
                    register.getDeptId()  ,
                    register.getDisease()  ,
                    register.getDrug()  ,
                    register.getFee()  ,
                    register.getGender()  ,
                    register.getHistory()  ,
                    register.getName()  ,
                    register.getPresent()  ,
                    register.getPresentTreat()  ,
                    register.getReadme()  ,
                    register.getRegistLevel()  ,
                    register.getStatus()  ,
                    register.getSuit()  ,
                    register.getUserid()  ,
                    register.getVisitTime()  ,
                    register.getRegistId() );
    }



    /**
    * 根据主键更新一条
    * @param id
    * @return
    */
    public int deleteById(Integer id){
        String sql = " delete from register  where regist_id = ?";
        return DBUtils.update( sql, id);

    }


}